import { configService } from '../../services/config-service';

export class FormatSettings {
  constructor() {
    this.settings = {
      // Markdown 设置
      markdown: {
        addTitle: false,
        preserveLineBreaks: true,
        addPageBreaks: false,
        useCodeBlocks: true,
        preserveImages: true,
        imageFormat: 'markdown'
      },
      // Word 设置
      word: {
        fontSize: 12,
        fontFamily: 'Arial',
        lineSpacing: 1.5,
        pageMargins: 'normal',
        includeImages: true,
        preserveFormatting: true
      },
      // PDF 设置
      pdf: {
        pageSize: 'A4',
        orientation: 'portrait',
        fontSize: 12,
        fontFamily: 'Arial',
        includeImages: true,
        quality: 'high'
      }
    };
    
    this.isLoading = true;
    this.errorMessage = '';
    this.successMessage = '';
    
    this.init();
  }

  async init() {
    try {
      await configService.loadConfig();
      this.loadSettings();
      this.isLoading = false;
    } catch (error) {
      console.error('初始化失败:', error);
      this.errorMessage = '初始化失败，请刷新页面重试';
      this.isLoading = false;
    }
  }

  loadSettings() {
    // 从本地存储加载设置
    const savedSettings = localStorage.getItem('formatSettings');
    if (savedSettings) {
      try {
        const parsed = JSON.parse(savedSettings);
        this.settings = { ...this.settings, ...parsed };
      } catch (error) {
        console.error('加载设置失败:', error);
      }
    }
  }

  saveSettings() {
    try {
      localStorage.setItem('formatSettings', JSON.stringify(this.settings));
      this.successMessage = '设置已保存';
      setTimeout(() => {
        this.successMessage = '';
      }, 3000);
    } catch (error) {
      console.error('保存设置失败:', error);
      this.errorMessage = '保存设置失败';
    }
  }

  resetSettings() {
    this.settings = {
      markdown: {
        addTitle: false,
        preserveLineBreaks: true,
        addPageBreaks: false,
        useCodeBlocks: true,
        preserveImages: true,
        imageFormat: 'markdown'
      },
      word: {
        fontSize: 12,
        fontFamily: 'Arial',
        lineSpacing: 1.5,
        pageMargins: 'normal',
        includeImages: true,
        preserveFormatting: true
      },
      pdf: {
        pageSize: 'A4',
        orientation: 'portrait',
        fontSize: 12,
        fontFamily: 'Arial',
        includeImages: true,
        quality: 'high'
      }
    };
    
    this.saveSettings();
  }

  updateMarkdownSetting(key, value) {
    this.settings.markdown[key] = value;
  }

  updateWordSetting(key, value) {
    this.settings.word[key] = value;
  }

  updatePdfSetting(key, value) {
    this.settings.pdf[key] = value;
  }

  getSettingsForFormat(format) {
    switch (format) {
      case 'md':
      case 'markdown':
        return this.settings.markdown;
      case 'docx':
      case 'word':
        return this.settings.word;
      case 'pdf':
        return this.settings.pdf;
      default:
        return {};
    }
  }

  exportSettings() {
    const dataStr = JSON.stringify(this.settings, null, 2);
    const dataBlob = new Blob([dataStr], { type: 'application/json' });
    const url = URL.createObjectURL(dataBlob);
    
    const link = document.createElement('a');
    link.href = url;
    link.download = 'format-settings.json';
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    
    URL.revokeObjectURL(url);
  }

  importSettings(event) {
    const file = event.target.files[0];
    if (!file) return;

    const reader = new FileReader();
    reader.onload = (e) => {
      try {
        const imported = JSON.parse(e.target.result);
        this.settings = { ...this.settings, ...imported };
        this.saveSettings();
        this.successMessage = '设置导入成功';
        setTimeout(() => {
          this.successMessage = '';
        }, 3000);
      } catch (error) {
        console.error('导入设置失败:', error);
        this.errorMessage = '导入设置失败，请检查文件格式';
      }
    };
    reader.readAsText(file);
  }

  getFontFamilyOptions() {
    return [
      { value: 'Arial', label: 'Arial' },
      { value: 'Times New Roman', label: 'Times New Roman' },
      { value: 'Calibri', label: 'Calibri' },
      { value: 'Helvetica', label: 'Helvetica' },
      { value: 'Georgia', label: 'Georgia' },
      { value: 'Verdana', label: 'Verdana' }
    ];
  }

  getPageSizeOptions() {
    return [
      { value: 'A4', label: 'A4' },
      { value: 'A3', label: 'A3' },
      { value: 'Letter', label: 'Letter' },
      { value: 'Legal', label: 'Legal' }
    ];
  }

  getOrientationOptions() {
    return [
      { value: 'portrait', label: '纵向' },
      { value: 'landscape', label: '横向' }
    ];
  }

  getQualityOptions() {
    return [
      { value: 'low', label: '低质量' },
      { value: 'medium', label: '中等质量' },
      { value: 'high', label: '高质量' }
    ];
  }

  getPageMarginsOptions() {
    return [
      { value: 'narrow', label: '窄边距' },
      { value: 'normal', label: '正常边距' },
      { value: 'wide', label: '宽边距' }
    ];
  }

  getImageFormatOptions() {
    return [
      { value: 'markdown', label: 'Markdown 格式' },
      { value: 'html', label: 'HTML 格式' },
      { value: 'base64', label: 'Base64 嵌入' }
    ];
  }
} 